package com.byl.modules.sys.controller;

import com.byl.common.modules.controller.BaseController;
import com.byl.common.validate.ValidateUtils;
import com.byl.modules.sys.service.LoginService;
import com.byl.modules.sys.service.UserService;
import com.byl.sdk.bean.ResultBean;
import com.byl.sdk.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by Administrator on 2018/11/30.
 */
@RestController
@RequestMapping("/api/sys/user")
public class UserController extends BaseController {

    @Autowired
    UserService userService;

    @Autowired
    LoginService loginService;

    @PostMapping("/list")
    public String listUser(@RequestBody Map map) {
        ResultBean bean = userService.listUser(map);
        return success("用户列表", bean);
    }

    @PostMapping("/save")
    public String saveUser(@RequestBody Map map) {
        if (!ValidateUtils.validatePassword(map))
            return fail("确认密码与密码输入不一致");
        int rs = userService.checkUser(map.get("username").toString());
        if (rs > 0)
            return success("该用户名已被占用！", "1");
        rs = userService.saveUser(map);
        if (rs > 0)
            return success("保存成功", "");
        return fail("保存失败");
    }

    @PostMapping("/edit")
    public String editUser(@RequestBody Map map) {
        int rs = userService.editUser(map);
        if (rs > 0)
            return success("保存成功", "");
        return fail("保存失败");
    }

    @GetMapping("/reset")
    public String resetpassword(String id){
        int rs=userService.resetpassword(id);
        if(rs>0)
            return success("重置密码","密码已重置为123456");
        return fail("重置失败");
    }

    @GetMapping("/delete")
    public String delUser(String id) {
        int rs = userService.delUser(id);
        if (rs > 0)
            return success("删除成功", "");
        return fail("删除失败");
    }

    @GetMapping("/change/status")
    public String chgUserStatus(String id) {
        int rs = userService.chgUserStatus(id);
        if (rs > 0)
            return success("操作成功", "");
        return fail("操作失败");

    }

    @GetMapping("/check")
    public String checkUser(String username) {
        int rs = userService.checkUser(username);
        if (rs > 0)
            return success("该用户名已被占用！", "1");
        return success("该用户名可以使用", "0");
    }


    @PostMapping("/password/update")
    public String updatePwd(@RequestBody Map map) {
        Object newPassword = map.get("newPassword");
        Object confirmPassword = map.get("confirmPassword");
        if (null == newPassword || null == confirmPassword)
            return fail("新密码或确认密码为空");
        if (!newPassword.toString().equals(confirmPassword.toString()))
            return fail("确认密码与新密码不一致");
        Map user = loginService.queryUser(map.get("username").toString());
        if (null == user)
            return fail("要修改的用户不存在");
        Object pwd = map.get("password");
        if (null == pwd || "".equals(pwd))
            return fail("原密码不能为空！");
        String enpwd = MD5Util.getMD5(pwd.toString());
        String dbpwd = user.get("password").toString();
        if (!enpwd.equals(dbpwd))
            return fail("原密码不正确！");
        int rs = userService.updatePassword(map);
        if (rs > 0)
            return success("密码修改成功", "");
        else
            return fail("密码修改失败");
    }

}
